<?php
/*
Template Name: 6. ShopperPress Callback
*/


$wpdb->hide_errors(); nocache_headers();

global $userdata; get_currentuserinfo(); // grabs the user info and puts into vars


/*

 THE CODE BELOW IS FOR PAYAL PAYMENTS PRO ONLY

*/
if(isset($_POST['gateway']) && $_POST['gateway'] == "paypalpro"){


	define('API_USERNAME', get_option('paypalpro_username'));
	define('API_PASSWORD', get_option('paypalpro_password'));
	define('API_SIGNATURE', get_option('paypalpro_sig'));
	require_once(FilterPath()."/wp-content/themes/shopperpress/functions/gateway_ideal/paypal_pro.inc.php");

	$firstName 			= urlencode( $_POST['firstName']);
	$lastName 			= urlencode( $_POST['lastName']);
	$creditCardType 	= urlencode( $_POST['creditCardType']);
	$creditCardNumber 	= urlencode($_POST['creditCardNumber']);
	$expDateMonth 		= urlencode( $_POST['expDateMonth']);
	$padDateMonth 		= str_pad($expDateMonth, 2, '0', STR_PAD_LEFT);
	$expDateYear 		= urlencode( $_POST['expDateYear']);
	$cvv2Number 		= urlencode($_POST['cvv2Number']);
	$address1 			= urlencode($_POST['address1']);
	$address2 			= urlencode($_POST['address2']);
	$city 				= urlencode($_POST['city']);
	$state 				= urlencode( $_POST['state']);
	$zip 				= urlencode($_POST['zip']);
	$amount 			= urlencode($_POST['amount']);
	$currencyCode		= get_option('paypalpro_currency');
	$paymentAction 		= urlencode("Sale");

	if($_POST['recurring'] == 1) // For Recurring
	{
		$profileStartDate = urlencode(date('Y-m-d h:i:s'));
		$billingPeriod = urlencode($_POST['billingPeriod']);// or "Day", "Week", "SemiMonth", "Year"
		$billingFreq = urlencode($_POST['billingFreq']);// combination of this and billingPeriod must be at most a year
		$initAmt = $amount;
		$failedInitAmtAction = urlencode("ContinueOnFailure");
		$desc = urlencode("Recurring $".$amount);
		$autoBillAmt = urlencode("AddToNextBilling");
		$profileReference = urlencode("Anonymous");
		$methodToCall = 'CreateRecurringPaymentsProfile';
		$nvpRecurring ='&BILLINGPERIOD='.$billingPeriod.'&BILLINGFREQUENCY='.$billingFreq.'&PROFILESTARTDATE='.$profileStartDate.'&INITAMT='.$initAmt.'&FAILEDINITAMTACTION='.$failedInitAmtAction.'&DESC='.$desc.'&AUTOBILLAMT='.$autoBillAmt.'&PROFILEREFERENCE='.$profileReference;
	}
	else
	{
		$nvpRecurring = '';
		$methodToCall = 'doDirectPayment';
	}

	$nvpstr='&PAYMENTACTION='.$paymentAction.'&AMT='.$amount.'&CREDITCARDTYPE='.$creditCardType.'&ACCT='.$creditCardNumber.'&EXPDATE='.         $padDateMonth.$expDateYear.'&CVV2='.$cvv2Number.'&FIRSTNAME='.$firstName.'&LASTNAME='.$lastName.'&STREET='.$address1.'&CITY='.$city.'&STATE='.$state.'&ZIP='.$zip.'&COUNTRYCODE=US&CURRENCYCODE='.$currencyCode.$nvpRecurring;
	$paypalPro = new paypal_pro(API_USERNAME, API_PASSWORD, API_SIGNATURE, '', '', FALSE, FALSE );
	$resArray = $paypalPro->hash_call($methodToCall,$nvpstr);
	$ack = strtoupper($resArray["ACK"]);

	if($ack != "SUCCESS")
	{
		$order_status = "error";

	}else{
		
		$post_data = $_POST;		
		UpdateOrderStatus(5, $_POST['paypal_order_id'], $post_data);
		$status['order_status'] = 5;
		$order_status = "thankyou";	
	} 
 	//die(print_r($resArray));
}
/*
	END PAYPAL PAYMENTS PRO
*/

/*

IDEAL PAYMENT GATEWAT

*/
if (isset($_GET['transaction_id']) ) {
	require_once('functions/gateway_ideal/ideal.class.php');
	$iDEAL = new iDEAL_Payment (get_option('ideal_ID'));	
	$iDEAL->checkPayment($_GET['transaction_id']);
	if ($iDEAL->getPaidStatus() == true){
	$post_data = $_POST;		
	UpdateOrderStatus(5, $_POST['paypal_order_id'], $post_data);
	$status['order_status'] = 5;
	$order_status = "thankyou";	
	}else{
	$order_status = "cancel";	
	}
}
/*
	END IDEAL
*/

//$_POST['mc_gross'] =1;
//$_POST['custom'] = '2403101269403908-1';
//$_POST['payment_status'] = "Completed";

//$_POST['x_response_code'] =1;
//$_POST['x_cust_id'] = "2001101263966016-1";
// check to see if the form has been posted. If so, validate the fields
 
if(!empty($_GET['order_id']) && !isset($_GET['force'])){

	$status = CheckPaymentComplete($_GET); 
	if($status['order_status'] == "5"){
	
		$order_status = "thankyou";
		$downloads = CheckDownloadLinks($status['order_items']);
		session_destroy();

	}else{
	
		$order_status = "error";
	}

}elseif(!empty($_POST) && !isset($_POST['paypal_order_id']) ){
  
	$which_gateway = CheckPaymentGateways($_POST); 
	$order_status = CheckPaymentStatus($which_gateway, $_POST);
	$order_data = CheckOrderData($_POST,$which_gateway);
	$downloads = CheckDownloadLinks($order_data['order_items']);
 
	
}	
if(isset($_GET['force'])){
$order_status = "thankyou";
}
@session_destroy();
get_header(); 
 
?>

 
<?php if($order_status == "thankyou"){ ?> 

<h2 class="h2top">Payment Complete</h2><br />
<h3>Thank you for your order.</h3>
<p>Your order has now been completed, you will be contacted by our team shortly.</p>

<?php if(!empty($downloads) && strlen($downloads[0]['file']) > 0 ){ ?>

<p><b>File Downloads Available</b></p>
<p>Listed below are all the files available to download with your order</p>
 
 <table id="gradient-style">
    <thead>
    	<tr>
        	<th scope="col">Name</th>
            <th scope="col">Qty</th>            
            <th scope="col" align="center">Actions</th>
        </tr>
    </thead>
    <tbody>
		
<?php  

// CHECK FOR DOWNLOAD LINKS

foreach($downloads as $data){
 
if(isset($data['file']) && strlen($data['file']) > 3){ 

?>        
	<tr>
        	<td><?php echo $data['name']; ?></td>
            <td><?php echo $data['qty']; ?></td>
        	<td align='center'>
            
            <form action="" method="POST" name="downloadform">
            <?php wp_nonce_field('ShopperPressDownload') ?>
            <input type="hidden" name="hash" value="123">
            <input type="hidden" name="fileID" value="<?php echo $data['id']; ?>">	 
               <a  href="#" onclick="document.downloadform.submit();" >
                    <?php echo SPEC($GLOBALS['_LANG']['_download']) ?>
               </a>             
            </form>
            
            </td>
    </tr>
        
<?php } } ?>
        
    </tbody>
</table>

<?php } ?>


<?php }elseif($order_status =="pending"){ ?> 

<h2 class="h2top">Payment Pending</h2><br />
<h3>Thank you for your order.</h3>
<p>Your order has now been completed but payment is pending approval, you will be contacted by our team shortly.</p>

<?php }elseif($order_status =="error"){ ?> 

<h2 class="h2top">Payment Failed</h2><br />
<h3>Sorry but there was an error during checkout.</h3>
<p>No money has been taken from your account, you will be contacted by our team shortly.</p>

<?php }else{ ?>

<h2 class="h2top">Order Cancelled</h2><br />
<h3>No money has been taken.</h3>
<p>No money has been taken from your account, you will be contacted by our team shortly.</p>


<?php } ?> 
 

<?php get_footer(); ?>